from rest_framework.generics import ListAPIView, ListCreateAPIView

from apps.meiduo_admin.serializers.user_serializers import UserModelSerializer
from apps.meiduo_admin.utils import PageNum
from apps.users.models import User


class UserListView(ListCreateAPIView):
    # 指定查询集
    # queryset = User.objects.all()

    # 指定序列化器
    serializer_class = UserModelSerializer
    # 分页类
    pagination_class = PageNum

    def get_queryset(self):
        # 获取前端传递的keyword值
        keyword = self.request.query_params.get('keyword')
        # 如果keyword是空字符，则说明要获取所有用户数据
        if keyword is '' or keyword is None:
            # 没有数据 就查询所有内容
            return User.objects.all()
        else:
            # 如果有数据 就过滤
            return User.objects.filter(username=keyword)
